<script setup>
import { ref, reactive, shallowRef, onMounted } from 'vue'
import { read, write, utils, version } from 'xlsx';
import { message, open, save } from '@tauri-apps/plugin-dialog';
import { readFile, writeFile } from '@tauri-apps/plugin-fs';

const ver = ref(version);
const data = shallowRef([])
const origin = ref("");

const update = (wb) => {
  const ws = wb.Sheets[wb.SheetNames[0]];
  const firstCell = ws[utils.encode_cell({ r: 0, c: 0 })];
  data.value = utils.sheet_to_json(ws, { header: 1 })
};

onMounted(async () => {
  try {
  } catch (e) { await message(e.message || e, { title: "Fetch Error", type: "error" }); }
});

const filters = reactive([
  { name: "Excel Workbook", extensions: ["xlsx"] },
  { name: "Excel 97-2004 Workbook", extensions: ["xls"] },
  { name: "Excel Binary Workbook", extensions: ["xlsb"] },
  { name: "Excel 2003 XML Spreadsheet", extensions: ["xml"] },
  { name: "Symbolic Link", extensions: ["slk"] },
  { name: "Flat OpenDocument Spreadsheet", extensions: ["fods"] },
  { name: "OpenDocument Spreadsheet", extensions: ["fods"] },
]);

/* Load from File */
const openFile = async () => {
  try {
    const selected = await open({
      title: "Open Spreadsheet",
      multiple: false,
      directory: false,
      filters
    });
    const d = await readFile(selected);
    const wb = read(d);
    update(wb);
    origin.value = selected;
  } catch (e) { console.log(e.message || e, { title: "Load Error", type: "error" }); }
};

/* Save to File */
const saveFile = async () => {
  try {
    const selected = await save({
      title: "Save to Spreadsheet",
      filters
    });
    if (!selected) throw new Error("No file selected");
    const ws = utils.aoa_to_sheet(data.value);
    const wb = utils.book_new();
    const range = utils.decode_range(ws['!ref']);
    const maxCol = range.e.c;
    ws['!merges'] = [{ s: { r: 0, c: 0 }, e: { r: 0, c: maxCol } }];
    utils.book_append_sheet(wb, ws, "SheetJSTauri");
    const d = write(wb, { type: "buffer", bookType: selected.slice(selected.lastIndexOf(".") + 1) });
    await writeFile(selected, d);
    await message(`File saved to ${selected}`);
  } catch (e) { await message(e.message || e, { title: "Save Error", type: "error" }); }
};
</script>

<template>
  <div class="root">
    <h1><a href="https://sheetjs.com" target="_blank">
        <img src="https://sheetjs.com/sketch128.png" class="logo" alt="SheetJS" />
        SheetJS × Tauri {{ ver }}</a></h1>

    <div class="centre"><button type="button" @click="openFile()">Load Data</button> or
      <button type="button" @click="saveFile()">Save Data</button>
    </div>
    <p class="centre"><b class="centre">Data from {{ origin }}</b></p>
    <table class="center">
      <tbody>
        <tr v-for="(row, index) in data" v-bind:key="index">
          <td v-for="(cell, col) in row" v-bind:key="col">
            {{ cell }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped>
.logo {
  padding: 0px;
  height: 64px;
  width: 64px;
  vertical-align: middle;
}

.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}

.centre {
  text-align: center;
}

table.center {
  margin-left: auto;
  margin-right: auto;
}

.root {
  font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 24px;
  font-weight: 400;

  color: #0f0f0f;
  background-color: #f6f6f6;

  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
}

a {
  font-weight: 500;
  color: #646cff;
  text-decoration: inherit;
}

a:hover {
  color: #535bf2;
}

h1 {
  text-align: center;
}

input,
button {
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0.6em 1.2em;
  font-size: 1em;
  font-weight: 500;
  font-family: inherit;
  color: #0f0f0f;
  background-color: #ffffff;
  transition: border-color 0.25s;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);
}

button {
  cursor: pointer;
}

button:hover {
  border-color: #396cd8;
}

button:active {
  border-color: #396cd8;
  background-color: #e8e8e8;
}

input,
button {
  outline: none;
}

@media (prefers-color-scheme: dark) {
  .root {
    color: #f6f6f6;
    background-color: #2f2f2f;
  }

  a:hover {
    color: #24c8db;
  }

  input,
  button {
    color: #ffffff;
    background-color: #0f0f0f98;
  }

  button:active {
    background-color: #0f0f0f69;
  }
}
</style>